package com.shouhuobao.bhi.stamp;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.text.InputFilter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.BaseActivity2;
import com.collectplus.express.R;
import com.collectplus.express.logic.AppConfig;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppLogic;
import com.collectplus.express.logic.AppResult;
import com.collectplus.express.logic.JSON;
import com.collectplus.express.logic.MsgID;
import com.collectplus.express.model.CouponBean;
import com.collectplus.express.wxapi.WXshare;

import droid.frame.view.InputFilterExt;
import droid.frame.view.TextWatcherExt;

/**
 * Created by joker on 2017/8/25.
 * 邮票转赠界面
 */
public class StampTransformActivity extends BaseActivity2 implements View.OnClickListener {

    private CouponBean couponBean = null;
    private EditText mPhoneET;
    private boolean isFriend = true;//true 微信好友  false 朋友圈

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.stamp_transform);
        super.findViewById();
        getAppTitle().setCommonTitle("邮票赠送");
        couponBean = (CouponBean) getIntent().getSerializableExtra("stampbean");
        AppLogic.getInstance().loadImage(couponBean.getImage(), (ImageView) findViewById(R.id.stamp_iv));
        ((TextView) findViewById(R.id.stamp_name_tv)).setText(couponBean.getName());
        ((TextView) findViewById(R.id.stamp_series_tv)).setText(couponBean.getSeries());
        if (isNotEmpty(couponBean.getExpireDate())) {
            ((TextView) findViewById(R.id.stamp_expiredate_tv)).setText("有效期至" + couponBean.getExpireDate());
        } else {
            if (isNotEmpty(couponBean.getValidStart()) && isNotEmpty(couponBean.getValidEnd())) {
                ((TextView) findViewById(R.id.stamp_expiredate_tv)).setText("有效期" + couponBean.getValidStart().substring(0, 10) + "至" + couponBean.getValidEnd().substring(0, 10));
            }
        }
        ((TextView) findViewById(R.id.stamp_use_conditions_tv)).setText("使用条件：" + couponBean.getCondition());
        mPhoneET = (EditText) findViewById(R.id.phone_et);
        final Button button = (Button) findViewById(R.id.transform_bt);
        button.setOnClickListener(this);
        setButtonEnableStyle(button, false);
        //分享给微信好友
        findViewById(R.id.share_wx_tv).setOnClickListener(this);
        //分享到微信朋友圈
        findViewById(R.id.share_wx_friendcircle_tv).setOnClickListener(this);
        mPhoneET.setFilters(new InputFilter[]{new InputFilterExt(1), new InputFilter.LengthFilter(13)});
        mPhoneET.addTextChangedListener(new TextWatcherExt(1) {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 13) {
                    setButtonEnableStyle(button, true);
                } else {
                    setButtonEnableStyle(button, false);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //立即赠送
            case R.id.transform_bt:
                if (isNotEmpty(mPhoneET.getText().toString().trim()) && mPhoneET.getText().length() == 13) {
                    showLoadingDialog(null);
                    AppHttp.getInstance().stampTransform(mPhoneET.getText().toString().replace(" ", ""), couponBean.getStampOrderId() + "");
                } else {
                    showToast("请输入正确的手机号码");
                }
                break;
            //分享给微信好友
            case R.id.share_wx_tv:
                isFriend = true;
                showLoadingDialog(null);
                AppHttp.getInstance().shareWXForStamp(couponBean.getStampOrderId());
                break;
            //分享到微信朋友圈
            case R.id.share_wx_friendcircle_tv:
                isFriend = false;
                showLoadingDialog(null);
                AppHttp.getInstance().shareWXForStamp(couponBean.getStampOrderId());
                break;

        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.stamp_transform:
                cancelLoadingDialog();
                final AppResult<?> result = JSON.parser(msg.obj);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (result.isSuccess()) {
                            showToast("转赠成功");
                            Intent intent = new Intent();
                            intent.putExtra("finish", true);
                            setResult(Activity.RESULT_OK, intent);
                            finish();
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return true;
            case MsgID.share_wx_for_stamp:
                cancelLoadingDialog();
                final AppResult<?> appResult = JSON.parseMap(msg.obj, Object.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (appResult.isSuccess()) {
                            Object stampPresentId = appResult.getResultMap().get("stampPresentId");
                            WXshare mWXshare = new WXshare(getContext());
                            String shareUrl = AppConfig.getServerUrl() + "static/promo/stamp.html?stampPresentId=" + stampPresentId + "&stampOrderId=" + couponBean.getStampOrderId();
                            String shareTitle = "您有一张火箭邮票待领取";
                            String shareDescription = "您的好友赠送了您一张《" + couponBean.getName() + "》邮票，可免费寄件使用";
                            Bitmap shareThumb = BitmapFactory.decodeResource(getResources(), R.drawable.wx_share_stamp);
                            mWXshare.shareWX(isFriend ? 0 : 1, shareTitle, shareDescription, shareThumb, shareUrl, null, null);
                        } else {
                            showToast(appResult.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

}
